package cn.liyicun.cp.cpweb.controller;

import cn.liyicun.cp.cpweb.base.KeyValue;
import cn.liyicun.cp.cpweb.entity.*;
import cn.liyicun.cp.cpweb.mapper.RechargeInfoMapper;
import cn.liyicun.cp.cpweb.service.MoneyLogService;
import cn.liyicun.cp.cpweb.service.SettingInfoService;
import cn.liyicun.cp.cpweb.service.UserInfoService;
import cn.liyicun.cp.cpweb.service.UserMoneyService;
import cn.liyicun.cp.cpweb.service.money.RechargeService;
import cn.liyicun.cp.cpweb.service.money.WithdrawService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = {"/money"})
public class MoneyController extends BaseController {

    @Autowired
    private RechargeInfoMapper rechargeInfoMapper;
    @Autowired
    private SettingInfoService settingInfoService;


    @Autowired
    MoneyLogService moneyLogService;
    @Autowired
    RechargeService rechargeService;
    @Autowired
    WithdrawService withdrawService;
    @Autowired
    UserInfoService userInfoService;
    @Autowired
    private UserMoneyService userMoneyService;

    //充值
    @RequestMapping(value = "/recharge")
    public String recharge(HttpServletRequest request, Map<String, Object> map) {
        Long uid = getLoginByIdError(request);

        List<KeyValue> rechargeTypes = settingInfoService.listCache("recharge_type");
        map.put("rechargeTypes", rechargeTypes);

        RechargeInfoExample exp = new RechargeInfoExample();
        exp.createCriteria().andStatusEqualTo(1);
        List<RechargeInfo> rechargeInfos = rechargeInfoMapper.selectByExample(exp);
        map.put("rechargeInfos", rechargeInfos);
        return "user/recharge";
    }


    //提现
    @RequestMapping(value = "/withdraw")
    public String withdraw(HttpServletRequest request, Map<String, Object> map) {
        Long uid = getLoginByIdError(request);
        UserInfo info = userInfoService.getUserInfo(uid);
        map.put("info", info);
        UserMoney moneyInfo = userMoneyService.getMoneyInfo(uid);
        if (moneyInfo != null) {
            map.put("money", moneyInfo.getMoney());
        }
        return "user/withdraw";
    }

    //流水
    @RequestMapping(value = "/list_money_log")
    public String list_money_log(HttpServletRequest request, Map<String, Object> map) {
        Long uid = getLoginByIdError(request);
        return "user/list_money_log";
    }


    @RequestMapping(value = "/searchRecharge")
    public String searchRecharge(HttpServletRequest request, Map<String, Object> map,

                                 @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                 @RequestParam(value = "size", required = false, defaultValue = "10") Integer size) {
        Long uid = getLoginByIdError(request);
        PageInfo<MoneyRecharge> pages = rechargeService.search(uid, page, size);
        map.put("pages", pages);
        return "user/searchRecharge";
    }

    @RequestMapping(value = "/searchWithdraw")
    public String searchWithdraw(HttpServletRequest request, Map<String, Object> map,
                                 @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                 @RequestParam(value = "size", required = false, defaultValue = "10") Integer size) {
        Long uid = getLoginByIdError(request);
        PageInfo<MoneyWithdraw> pages = withdrawService.search(uid, page, size);
        map.put("pages", pages);
        return "user/searchWithdraw";
    }

    @RequestMapping(value = "/searchMoneyLog")
    public String searchMoneyLog(HttpServletRequest request, Map<String, Object> map,
                                 @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                 @RequestParam(value = "size", required = false, defaultValue = "10") Integer size) {
        Long uid = getLoginByIdError(request);
        PageInfo<MoneyLog> pages = moneyLogService.search(uid, page, size);
        map.put("pages", pages);
        return "user/searchMoneyLog";
    }
}
